import coursewareApi from "@rc/apis/rc/courseware.mjs";

export default {
    methods: {
        /**
         * 执行点击事件
         *
         * @param {string} eventName 事件
         * @param {object} resource 资源
         */
        handleClick(eventName,resource){
            if(eventName==='onDelete'){
                this[eventName](resource);
            } else {
                this[eventName](resource.resourceId);
            }
        },
        /**
         * 课件批量质检
         *
         * @param {string} resourceId 资源Id
         */
         qualityCourseWare(resourceId){

            if (!resourceId && !this.selectIds.length) {
				this.$message.warning("请先选择课件!");
				return;
			}
            this.isShowBatchQualityLoading = true;
            const resourceIds = resourceId?[resourceId]:this.selectIds;
            coursewareApi.batchQualityCourseWare({
                data: {
                    resourceIds
                },
                success: (data) => {
                    if(data){
                        this.$message.success('质检操作成功');
                        this.onSubmit();
                    } else {
                        this.$message.error('质检操作失败');
                    }

                },
                complete: () => {
                    this.isShowBatchQualityLoading = false;
                }
            });
        },
         /**
          * 点击快速搜素区域内容，搜索子项
          *
          * @param {object} quickItem quickItem
          * @param {object}  value value 
          */
        getChildrenData(quickItem, value) {
            let project = this.$refs.projectTreeRef.api_getCurrentNode();
            if(!project ) {
                this.$message.warning("请先选择项目");
                return;
            }
            if(quickItem.key === 'teachRange' ) {
                this.getSeriesCourse(project.projectId, value);
            }
        },
        /**
         * 刷新
         */
        onSubmit() {
            this.$refs.projectTreeRef.api_getProjectCount();
            this.resourceQueryPagination.pageIndex = 0;
            this.onSearchResource();
        },
        /**
         * 搜索获取资源列表
         */
        onSearchResource() {
            return new Promise((resolve) => {
                 // 清除列表上的选择项
                this.isCheckedAll = false;
                this.selectItems = [];

                let params = {};
                // -------拼接参数----开始
                this.isShowLoading_table = true;
                let project = this.$refs.projectTreeRef.api_getCurrentNode();
                if(!project) {
                    this.$message.warning("请先选择项目");
                    return;
                }
                this.resourceList = [];
                //快速搜索区域参数
                const quickList = this.quickList;
                //筛选出出星级标签的参数
                const notlableParams = quickList.filter( item => !item.isLabel);
                const quickParams = this.$refs.quickScreenRef.quickParams;
                let quickListParams = {};
                //快速搜索区域-非标签参数
                notlableParams.forEach(param => {
                    quickListParams[param.key] = quickParams[param.key] || '';
                });
                const searchParams = this.$refs.veFiltrate.getResult();
                // 处理上传时间
                if(searchParams.uploadDate){
                    const {start,end} = searchParams.uploadDate;
                    searchParams.uploadStarTime= start?start+" 00:00:00":'';
                    searchParams.uploadEndTime= end?end+" 23:59:59":'';
                } else { //默认最近一周
                    searchParams.uploadStarTime= this.formatDateStart(this.beforeWeek())+" 00:00:00";
                    searchParams.uploadEndTime= this.formatDateNow(new Date)+" 23:59:59";
                }
                delete searchParams.uploadDate;
                params = {
                    projectId: project.projectId,
                    ...quickListParams,
                    ...searchParams,
                    pageIndex: PageUtil.getStartIndex(this.resourceQueryPagination),
                    pageSize: this.resourceQueryPagination.pageSize,
                };
                // -------拼接参数----结束
                coursewareApi.queryPendResourceVideoList({
                    data: {
                        ...params
                    },
                    success: (result) => {
                        result.data.forEach((item) => {
                            item.qualityStatusLabel = this.qualityStatusObj ? this.qualityStatusObj[item.qualityStatus] : '';
                        });
                        this.resourceList = result.data;
                        this.$set(this.resourceQueryPagination,'pageCount', result.count);
                    },
                    complete:(()=>{
                        this.isShowLoading_table = false;
                        resolve();
                    })
                });
            });

        },
        /**
         * 项目切换
         */
        onChooseProject() {
            this.getSubjectListByProjectId();
            const teachRange = this.$refs.quickScreenRef.quickParams.teachRange;
            this.getChildrenData({
                key :'teachRange',
            },teachRange);
        },
        /**
         * 编辑弹窗,项目信息部分点击提交
         *
         * @param {object} data 表单基础数据
         * @param {object} itemsDetail 表单内的下拉选中详情
         * @param {Function} callback 提交回调，用于告诉子组件是否提交成功
         */
		projectEditSubmit(data, itemsDetail, callback) {
			const { project, subject, course, year, period } = itemsDetail;
			const courseEnd = course.filter((i) => i.level === 2);
			// 批量编辑和单个编辑相同的字段部分
			const params = {
				projectId: data.projectId,
				projectName: project.find((i) => i.value === data.projectId).label,
				examCycleId: period.pypId,
				examCycleName: period.pypseqName,
				subjectList: subject.map((i) => ({ subjectId: i.subjectId, subjectName: i.subjectName })),
				seriesCourseList: courseEnd.map((i) => ({
					seriesCourseId: i.value,
					seriesCourseName: i.label,
					teachRange: i.parent.value
				})),
				applicableYear: year.projectyearYear,
				applicableYearId: year.projectyearId
			};
			this.projectLoading = true;
			if (this.isEditBatchStatus) {
				// 当前是批量编辑
				coursewareApi.editFileProjectBatch({
					data: {
						...params,
						resourceIds: this.selectIds
					},
					success: () => {
						callback(true);
						this.$message.success("修改成功");
						this.queryTableData(true);
					},
					complete: () => {
						this.projectLoading = false;
					}
				});
			} else {
				// 当前是单个编辑
				coursewareApi.editFileProject({
					data: {
						...params,
						resourceId: this.editInfo.resourceId,
						originalProjectId: data.originalProjectId || data.projectId
					},
					success: () => {
						callback(true);
						this.$message.success("修改成功");
						this.queryTableData();
					},
					complete: () => {
						this.projectLoading = false;
					}
				});
			}
		},
		/**
		 * 编辑弹窗,项目信息部分已保存的项目点击删除
		 *
		 * @param {string} projectId 要删除的项目id
		 * @param {Function} callback 提交回调，用于告诉子组件是否提交成功
		 */
		projectEditDelete(projectId, callback) {
			this.projectLoading = true;
			coursewareApi.delFileProjectBatch({
				data: {
					projectId,
					resourceIds: this.isEditBatchStatus ? this.selectIds : [this.editInfo.resourceId]
				},
				success: () => {
					callback(true);
					this.$message.success("删除成功");
				},
				complete: () => {
					this.projectLoading = false;
				}
			});
		},
        /**
         * 编辑弹窗,基础信息部分点击提交
         *
         * @param {object} data 表单基础数据
         * @param {object} itemsDetail 表单内的下拉选中详情
         */
		baseEditSubmit(data, itemsDetail) {
			// 批量编辑和单个编辑相同的字段部分
			const params = {
				teacherId: data.teacherId,
				professorId: itemsDetail.teacher.userId,
				professorName: itemsDetail.teacher.userName,
				professorDptName: itemsDetail.teacher.dptName || "无",
                livePlaybackState: data.livePlaybackState
			};
			this.baseLoading = true;
			if (this.isEditBatchStatus) {
				// 当前是批量编辑
				coursewareApi.editResourceBaseBatch({
					data: {
						...params,
						resourceIds: this.selectIds
					},
					success: () => {
						this.$message.success("修改成功");
						this.queryTableData(true);
					},
					complete: () => {
						this.baseLoading = false;
					}
				});
			} else {
				// 当前是单个编辑
				coursewareApi.editResourceBase({
					data: {
						...params,
						resourceId: this.editInfo.resourceId,
						name: data.name,
						sectionNumber: data.sectionNumber,
						auditState: "W", // "W" 待审核
                        sourceFileName: data.sourceFileName
					},
					success: () => {
						this.$message.success("修改成功");
						this.queryTableData(true);
					},
					complete: () => {
						this.baseLoading = false;
					}
				});
			}
		},
    }
};
